package org.krishna.spring.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.krishna.Dto.Employee;
import org.krishna.Dto.User;
import org.krishna.json.CreateDetails;
import org.krishna.mail.SendMail;
import org.krishna.service.ServiceHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;


/**
 * Handles requests for the Student service.
 */
@Controller
public class StudentController extends LMSContoller {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(StudentController.class);
	
	/** The service handler. */
    @Autowired
    @Qualifier("serviceHandler")
    private ServiceHandler serviceHandler;
    
    @Autowired
    @Qualifier("sendMail")
    private SendMail sendMail;
	
	//Map to store employees, ideally we should use database
	Map<Integer, Employee> empData = new HashMap<Integer, Employee>();
	
	@RequestMapping(value = StudentRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)
	public @ResponseBody Employee getDummyEmployee() {
		LOGGER.info("Start getDummyEmployee");
		Employee emp = new Employee();
		emp.setId(9999);
		emp.setName("Dummy");
		emp.setCreatedDate(new Date());
		empData.put(9999, emp);
		return emp;
	}
	
	@RequestMapping(value = StudentRestURIConstants.GET_EMP, method = RequestMethod.GET)
	public @ResponseBody Employee getEmployee(@PathVariable("id") int empId) {
		LOGGER.info("Start getEmployee. ID="+empId);
		
		return empData.get(empId);
	}
	
	@RequestMapping(value = StudentRestURIConstants.GET_ALL_EMP, method = RequestMethod.GET)
	public @ResponseBody List<Employee> getAllEmployees() {
		LOGGER.info("Start getAllEmployees.");
		List<Employee> emps = new ArrayList<Employee>();
		Set<Integer> empIdKeys = empData.keySet();
		for(Integer i : empIdKeys){
			emps.add(empData.get(i));
		}
		return emps;
	}
	
	@RequestMapping(value = StudentRestURIConstants.CREATE_EMP, method = RequestMethod.POST)
	public @ResponseBody Employee createEmployee(@RequestBody Employee emp) {
		LOGGER.info("Start createEmployee.");
		emp.setCreatedDate(new Date());
		empData.put(emp.getId(), emp);
		return emp;
	}
	
	@RequestMapping(value = StudentRestURIConstants.DELETE_EMP, method = RequestMethod.PUT)
	public @ResponseBody Employee deleteEmployee(@PathVariable("id") int empId) {
		LOGGER.info("Start deleteEmployee.");
		Employee emp = empData.get(empId);
		empData.remove(empId);
		return emp;
	}
	
	@RequestMapping(value = StudentRestURIConstants.REGISTER, method = RequestMethod.GET)
	public @ResponseBody Boolean createUser(@RequestParam(value = "create", required = true) String createJson) {
		LOGGER.info("Start creating a User.");
		CreateDetails details = (CreateDetails) getJavaObjectFromJson(
				createJson, new TypeReference<CreateDetails>() {
				});
		return serviceHandler.registerUser(details);		
	}
	
	@RequestMapping(value = StudentRestURIConstants.GETUSER, method = RequestMethod.GET)
	public @ResponseBody User getUser(@PathVariable("name") String userName) {
		LOGGER.info("UserName recieved is "+userName);		
		return serviceHandler.getUserByUserName(userName);		
	}
	
	@RequestMapping(value = StudentRestURIConstants.GETUSERS, method = RequestMethod.GET)
	public @ResponseBody List<User> getUsers() {
		LOGGER.info("Requesting student details");		
		return serviceHandler.getAllStudents();		
	}
	
	@RequestMapping(value = StudentRestURIConstants.UPDATEUSER, method = RequestMethod.GET)
	public @ResponseBody Boolean updateUser(@PathVariable("name") String name,
			@RequestParam(value = "mobile", required = true) String mobileNo,
			@RequestParam(value = "mail", required = true) String email) {
		LOGGER.info("Recieved request for updating student details "+name);	
		return serviceHandler.updateUser(name, mobileNo, email);		
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public @ResponseBody Boolean addBook(@PathVariable("name") String name,
			@RequestParam(value = "mobile", required = true) String mobileNo,
			@RequestParam(value = "mail", required = true) String email) {
		LOGGER.info("Recieved request for updating student details "+name);		
		return serviceHandler.updateUser(name, mobileNo, email);		
	}
	
}
